home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
xlib
/
font.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
5KB
|
160 lines
/*
* Copyright (c) 1993-94, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include <GL/glx.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* prototype */
void makeRasterFont(Display **dpy);
static long W = 300, H = 300;
static int attributeList[] = {GLX_RGBA, None};
static Bool WaitForNotify (Display *d, XEvent *e, char *arg)
{
return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
}
GLuint base;
void makeRasterFont(Display **dpy)
{
XFontStruct *fontInfo;
Font id;
unsigned int first, last;
fontInfo = XLoadQueryFont(*dpy,
"-adobe-courier-bold-o-normal--20-140-100-100-m-110-iso8859-1");
if (fontInfo == NULL) {
printf("no font found\n");
exit(0);
}
id = fontInfo->fid;
first = fontInfo->min_char_or_byte2;
last = fontInfo->max_char_or_byte2;
base = glGenLists(last+1);
if (base == 0) {
printf("out of display lists\n");
exit(0);
}
glXUseXFont(id, first, last-first+1, base+first);
}
void printString(char *s)
{
glPushAttrib(GL_LIST_BIT);
glListBase(base);
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (unsigned char *)s);
glPopAttrib();
}
void gldrawing(void) {
glLoadIdentity();
glOrtho(-500.0, 500.0, -500.0, 500.0, 0.0, 1.0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.9, 0.3, 0.9);
glBegin(GL_POLYGON);
glVertex3f( 0.0, -100.0, 0.0);
glVertex3f( 0.0, 100.0, 0.0);
glVertex3f(-200.0, 100.0, 0.0);
glVertex3f(-200.0, -100.0, 0.0);
glEnd();
glColor3f(0.9, 0.9, 0.9);
glRasterPos2f(-100.0, 0.0);
printString("Here's the origin.");
glRasterPos2f(-200.0, 102.0);
printString("HERE'S THE UPPER LEFT CORNER.");
glFlush();
}
main(int argc, char **argv)
{
Display *dpy; /* X Display */
XColor acolor; /* Color definitions */
XVisualInfo *vi; /* X Visual */
Colormap cmap; /* X Colormap */
XSetWindowAttributes swa; /* X Window Attributes */
Window win; /* X Window Id */
GLXContext cx; /* Extesion to X server; GLX context
Data structure for X to hold GL global
data, so that we don't have to send them
to X through protocal everytime to
eliminate X traffic */
XEvent event, report; /* X event */
int value;
/* Standard code to open a X window */
dpy = XOpenDisplay (0);
XSynchronize(dpy, True);
/* get a appropriate visual */
vi = glXChooseVisual (dpy, DefaultScreen(dpy), attributeList);
/* create a GLX context */
cx = glXCreateContext (dpy, vi, None, GL_FALSE);
/* create a colormap using this visual */
cmap = XCreateColormap (dpy, RootWindow(dpy, vi->screen), vi->visual,
AllocNone);
/* create a X Window */
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask | KeyPressMask;
win = XCreateWindow (dpy, RootWindow(dpy,vi->screen), 0, 0, 500, 500,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa);
XSelectInput(dpy, win, ExposureMask | KeyPressMask |
ButtonPressMask | StructureNotifyMask);
XMapWindow (dpy,win);
/* wait for window to map */
XIfEvent (dpy, &event,WaitForNotify, (char *)win);
/* connect the context to the window */
if (!glXMakeCurrent(dpy,win,cx) == GL_TRUE) {
return 0;
}
/* make font */
makeRasterFont(&dpy);
while (1) {
XNextEvent(dpy, &report);
switch (report.type) {
case KeyPress:
XCloseDisplay(dpy);
exit(1);
break;
case Expose:
gldrawing();
break;
default:
break;
} /*end switch*/
} /*end while*/
}